package com.sdemo.modules.sys.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sdemo.modules.comm.controller.BaseController;
import com.sdemo.modules.sys.model.Dept;
import com.sdemo.modules.sys.model.TreeDept;
import com.sdemo.modules.sys.service.impl.DeptServiceImpl;
import com.sdemo.utils.Ret;

@Controller
@RequestMapping("/sys")
public class DeptController extends BaseController {
	
	
	@Autowired
	private DeptServiceImpl deptServiceImpl; 
	
	/**
	 * 查询当前登录用户所在部门以及子部门
	 * @param userId
	 * @return
	 */
	@RequestMapping(value={"/queryDeptByUserId"})
	@ResponseBody
	public Ret queryDeptByUserId(Dept dept){
		dept.setId(getUserDeptId());
		List<TreeDept> list = deptServiceImpl.queryDeptByUserId(dept);
		if(list == null){
			Ret.error("数据为空");
		}
		return Ret.ok().put("data", list);
	}
	
	
	/**
	 * 查询部门信息
	 * @param userId
	 * @return
	 */
	
	@RequestMapping(value={"/queryDept"})
	@ResponseBody
	public Ret queryDept(Long userId){
		List<Dept> list = deptServiceImpl.queryDept();
		if(list == null){
			Ret.error("数据为空");
		}
		return Ret.ok().put("data", list);
	}
	
	/**
	 * 
	 * 修改角色管理中的部门信息(弹框动态加载部门信息)
	 * @return
	 */
	@RequestMapping(value={"/queryTreeDept"})
	@ResponseBody
	public Ret queryDept(){
		List<Dept> list = deptServiceImpl.queryDept();
		if(list == null){
			Ret.error("数据为空");
		}
		return Ret.ok().put("data", list);
	}
	
	/**
	 * 
	 * 新增部门信息
	 * @return
	 */
	@RequestMapping(value={"/insertDept"})
	@ResponseBody
	public Ret insertDept(Dept dept){
		dept.setCreateuid(1l);
		int index = deptServiceImpl.insertDept(dept);
		Ret ret = new Ret();
		if(index < 0){
			return ret.put("code", 0);
		}
		return ret.put("code", 1);
	}
	
	
	
}
